package cn.hs.ph.serviet;

import cn.hs.ph.dao.impl.ShopDaoimpl;
import cn.hs.ph.entity.Shop;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/shop")
public class ShopServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        ShopDaoimpl dao = new ShopDaoimpl();
        if(action.equals("selectAll")){
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String shopName = req.getParameter("shopName");
            Map<String,String> map = new HashMap<>();
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("shopName",shopName);
            List<Shop> list=null;

            try {
                list=dao.selectAll(map);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            int total=0;
            try {
                total=dao.selectCount(map);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            HashMap hashMap = new HashMap();
            hashMap.put("total",total);
            hashMap.put("list",list);

            Result success = Result.success(hashMap);
            resp.getWriter().write(JSON.toJSONString(success));


        }else if (action.equals("add")){
            String shopAddress = req.getParameter("shopAddress");
            String shopCity = req.getParameter("shopCity");
            String shopDescribe = req.getParameter("shopDescribe");
            String shopDistrict = req.getParameter("shopDistrict");
            String shopImage = req.getParameter("shopImage");
            String shopLatitude = req.getParameter("shopLatitude");
            String shopLongitude = req.getParameter("shopLongitude");
            String shopName = req.getParameter("shopName");
            String shopPhone = req.getParameter("shopPhone");
            String shopProvince = req.getParameter("shopProvince");
Shop shop = new Shop(shopAddress,shopCity,shopDescribe,shopDistrict,shopImage,shopLongitude,shopLatitude,shopProvince,shopPhone,shopName);
            int insert = 0;
            try {
                insert = dao.insert(shop);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            Result result=null;
            if(insert>0){
                result=Result.success("添加成功");

            }else {
                result=Result.error("添加失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));

        }else if (action.equals("update")){
            String Id = req.getParameter("shopId");
            int shopId=Integer.parseInt(Id);
            String shopAddress = req.getParameter("shopAddress");
            String shopCity = req.getParameter("shopCity");
            String shopDescribe = req.getParameter("shopDescribe");
            String shopDistrict = req.getParameter("shopDistrict");
            String shopImage = req.getParameter("shopImage");
            String shopLatitude = req.getParameter("shopLatitude");
            String shopLongitude = req.getParameter("shopLongitude");
            String shopName = req.getParameter("shopName");
            String shopPhone = req.getParameter("shopPhone");
            String shopProvince = req.getParameter("shopProvince");
          Shop shop=new Shop(shopId,shopAddress,shopCity,shopDescribe,shopDistrict,shopImage,shopLongitude,shopLatitude,shopProvince,shopPhone,shopName);
            int insert = 0;
            try {
                insert = dao.update(shop);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            Result result=null;
            if(insert!=0){
                result=Result.success("修改成功");

            }else {
                result=Result.error("修改失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));



        }






    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);

    }
}
